/*
 *
 *  Copyright (C) 2010 GSyC/LibreSoft, Universidad Rey Juan Carlos
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *
 *  Author : Roberto Calvo Palomino <rocapal@libresoft.es>
 *
 */


package com.LibreGeoSocial.App.AlarmManager;

import java.io.Serializable;
import java.util.ArrayList;

import android.location.Location;
import android.util.Log;

import com.LibreGeoSocial.Api.GeoNode;
import com.LibreGeoSocial.Api.LibreGeoSocial;
import com.LibreGeoSocial.App.Utils.LocationUtils;


public class NodeAlarm extends GenericAlarm implements Serializable {

	private static final long serialVersionUID = -6650042683017287017L;
	
	private GeoNode mNode;
	private Double  mDistance; // in meters
	
	public NodeAlarm (String name, GeoNode node, Double distance)
	{
		super(name);
		mNode = node;
		mDistance = distance * 1000; 
	}
	
	public GeoNode getNode()
	{
		return mNode;
	}
	
	public Double getDistance ()
	{
		return mDistance;
	}
	
	@Override
	public ArrayList<GeoNode> refreshData(Location CurrentLocation) {
		
		if (CurrentLocation == null)
		{
			Log.e("NodeAlarm","CurrentLocation==null!");
			return null;
		}
		ArrayList<GeoNode> nodeList = new ArrayList<GeoNode>();
		
		//updated Node
		mNode = LibreGeoSocial.getInstance().updateNode(mNode);
		
		if (mNode == null)
		{
			Log.e("NodeAlarm","mNode==null!");
			return null;
		}
		
		// Compare the locations
		if ( LocationUtils.calculateDistance(CurrentLocation, mNode.getLocation()) < mDistance)
		{
			nodeList.add(mNode);
		}
		
		
		return nodeList;
	}

}
